<?php


namespace app\model;


use app\common\Page;

class AppModel extends ModelBase
{
    protected $name = 'base_app';
    protected $pk = 'app_id';
    protected $pk_name = 'APP ID';
    protected $hav_deleted_column=0;
    protected $checkField = [
        ['column'=>'app_name','name'=>'应用名称']
    ];


    public function lst($where=[],$order='app_id asc'){
        $count = $this->where($where)->count();
        $page = new Page($count,input('limit',$count),input('page',1));
        $goodsLst = $this->where($where)->limit($page->firstRow , $page->listRows)->order($order)->select();
        $res['code'] = 0;
        $res['list'] = $goodsLst;
        $res['count'] = $count;
        $res['totalPages'] = $page->totalPages;
        return $res;
    }

    public function add($data=[]){
        $_data = array_merge((json_decode(rawBody(),true) ?? []),$data);
        if(empty($_data)){
            return error(1,'请求数据不能为空');
        }
        foreach($this->checkField as $field){
            if($this->checkFieldExistence($field['column'],$_data[$field['column']])){
                return error(1,$field['name'].'"'.$_data[$field['column']].'"已存在');
            }
        }
        $res = $this->insertGetId($_data);
        return $res;
    }
}